<template>
	<div class="padding-container preview-wrap">
		<div class="title-part">
      <el-button size="mini" type="primary" class="pull-right" @click="toCreate" v-show="showBtn">{{ $root.lang.control.device.list.add }}</el-button>
		</div>
		<el-form ref="tableParams" :inline="true" :model="tableParams" size="small">
      <el-form-item :label="$root.lang.control.device.list.deviceName">
        <el-input v-model="tableParams.deviceName"></el-input>
      </el-form-item>
      <el-form-item :label="$root.lang.control.device.list.IMEI">
        <el-input v-model="tableParams.imei"></el-input>
      </el-form-item>
      <el-form-item :label="$root.lang.control.device.list.activeState">
        <el-select v-model="tableParams.isActivation" :placeholder="$root.lang.control.device.list.select" clearable>
          <el-option
            v-for="item in setState.activeState"
            :key="item.value"
            :label="item.title"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$root.lang.control.device.list.connectState">
        <el-select v-model="tableParams.isConnection" :placeholder="$root.lang.control.device.list.select" clearable>
          <el-option
            v-for="item in setState.connectState"
            :key="item.value"
            :label="item.title"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$root.lang.control.device.list.bindState">
        <el-select v-model="tableParams.isBinding" :placeholder="$root.lang.control.device.list.select" clearable>
          <el-option
            v-for="item in setState.bindState"
            :key="item.value"
            :label="item.title"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="searchAction()">{{ $root.lang.control.device.list.query }}</el-button>
        <el-button size="small" @click="searchAction(true)">{{ $root.lang.control.device.list.empty }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="deviceList"
      :empty-text="$root.lang.control.device.list.noData"
      size="small"
      border
      style="width: 100%"
      >
      <el-table-column
        align="center"
        :label="$root.lang.control.device.list.deviceName">
        <template slot-scope="scope">
          <span>{{ scope.row.deviceName | dataFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$root.lang.control.device.list.IMEI">
        <template slot-scope="scope">
          <span>{{ scope.row.imei | dataFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="210"
        align="center"
        :label="$root.lang.control.device.list.appInfo">
        <template slot-scope="scope">
          <span v-if="scope.row.appDetails.length == 0">--</span>
          <p v-if="scope.row.appDetails.length > 0" style="text-align: left;" v-for="(itemApp, index) in scope.row.appDetails" :key="index"><span style="margin-right:15px;">{{ index+1 }}.</span>
          {{ itemApp.appIdentification }}&nbsp;&nbsp;&nbsp; {{ itemApp.appV }} &nbsp;&nbsp;&nbsp; {{ baseState.appState, itemApp.state ,'--' | matchValue }} </p>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$root.lang.control.device.list.activeState">
        <template slot-scope="scope">
          <p>{{ setState.activeState, scope.row.isActivation, '--' | matchValue }}</p>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$root.lang.control.device.list.connectState">
        <template slot-scope="scope">
          <p>{{ setState.connectState, scope.row.isConnection, setState.connectState["0"].title | matchValue }}</p>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$root.lang.control.device.list.bindState">
        <template slot-scope="scope">
          <p>{{ setState.bindState, scope.row.isBinding, '--' | matchValue }}</p>
        </template>
      </el-table-column>
      <el-table-column
        width="140"
        align="center"
        :label="$root.lang.control.device.list.lastHeartTime">
        <template slot-scope="scope">
          <span>{{ scope.row.lastHeartTime | dateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column 
        width="220"
        align="center"
        :label="$root.lang.control.device.list.operate">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="toDetail(scope.$index, scope.row)">{{ $root.lang.control.device.list.look }}</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="toDelete(scope.$index, scope.row)" v-show="showBtn">{{ $root.lang.control.device.list.remove }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="tableTotal>0"
      @size-change="sizeChange"
      @current-change="pageChange"
      @prev-click="pageChange"
      @next-click="pageChange"
      :current-page="tableParams.pageNum"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="tableParams.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="tableTotal">
    </el-pagination>

    <DeviceChose :show="showChose" :groupId="groupId" @receive="getReceive" @reload="reloadTable" />
	</div>
</template>

<script>
import DeviceChose from '@/views/controlCenter/deviceManage/group/components/DeviceChose'
import '@/options/deviceState'
import '@/options/baseState'
export default {
	name: 'DeviceList',
  components: {
    DeviceChose
  },
	data() {
		return {
			listLoading: false,
			value: '',
      tableParams:{
				pageNum: 1,
				pageSize: 10,
				deviceName: null,
				imei: null,
				isConnection: null,
        isActivation: null,
        isBinding: null,
        delFlag: 0,
        groupId: this.groupId,
        deviceState: 0,
			},
      tableData: [],
      tableTotal: 0,
      setState: {},
      baseState: {},
      showChose: false,
      deviceList: null,
      showBtn: true
		}
	},
  props: {
    groupId: {
      type: String,
      default: null
    }
  },
  methods: {
    getReceive(data) {
      this[data.key] = data.value
    },
    checkLang() {
      let langType = this.$root.getStorage('lang')
      if(langType == 'en') {
        this.setState = enDeviceConfigData
        this.baseState = enBaseState
      } else {
        this.setState = deviceConfigData
        this.baseState = baseState
      }
    },
    toCreate() {
      this.showChose = true
    },
    reloadTable() {
      this.$emit('reload')
      this.searchAction(true)
      this.showChose = false
    },
    sizeChange(val) {
      this.tableParams.pageNum = 1
      this.tableParams.pageSize = val
      this.fetchData()
    },
    pageChange(val) {
      this.tableParams.pageNum = val
      this.fetchData()
    },
    toDetail(index,row) {
      this.$router.push({ name:'DeviceDetail', params:{ id: row.id } })
    },
    toDelete(index,row) {
      this.$confirm(this.$root.lang.confirm.removeDevice, this.$root.lang.confirm.tips, {
        confirmButtonText: this.$root.lang.confirm.removeImmediately,
        cancelButtonText: this.$root.lang.confirm.notRemove,
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: this.$root.lang.confirm.process,
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        this.request({
          url: '/device/remove/group?id=' + row.id,
          method: 'get',
        }).then(response => {
          loading.close()
          this.$message({
            message: this.$root.lang.confirm.operateSuccess,
            type: 'success',
            duration: 3 * 1000
          })
          this.reloadTable()
        }).catch(error => {
          loading.close()
          this.$message({
            message: error.errmsg,
            type: 'error',
            duration: 3 * 1000
          })
        })
      }).catch(() => {
        
      })
    },
    searchAction(reset) {
      if(reset) {
        this.tableParams = {
          pageNum: 1,
          pageSize: 10,
          deviceName: null,
          imei: null,
          isConnection: null,
          isActivation: null,
          isBinding: null,
          delFlag: 0,
          groupId: this.groupId,
          deviceState: 0,
        }
      } else {
        this.tableParams.pageNum = 1
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      this.request({
          url: '/device/page',
          method: 'post',
          data: this.$root.optionProcess(this.tableParams)
        }).then(response => {
          this.deviceList = response.rows
          this.tableTotal = response.total
          this.listLoading = false
        }).catch(error => {
          this.deviceList = []
          this.listLoading = false
          this.$message({
            message: error.errorMessage,
            type: 'error',
            duration: 3 * 1000
          })
      })
    },
  },
  created() {
    if(this.Cookies.get('T') == '2') {
      if(this.Cookies.get('O') == '1') {
        this.showBtn = false
      }
		} 
    this.checkLang()
    this.fetchData();
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
	.padding-container {
		text-align:left;
		.title-part {
			text-align: left;
			font-size:16px;
			padding-bottom:30px;
		}
	}
</style>